package com.dg.gaming.client.gui
{
	import com.dg.gaming.api.common.util.DGLabels;
	import com.dg.gaming.api.common.util.GameProperties;
	import com.dg.gaming.api.common.util.LoaderCollection;
	import com.dg.gaming.api.common.util.LoaderCollectionEvent;
	import com.ericfeminella.collections.HashMap;

	import flash.display.BitmapData;

	public class GameImages
	{

		private var finishedLoading:Boolean=false;

		private var imageHash:HashMap=new HashMap();

		private static var instance:GameImages=null;

		/**
		 * <p>Return a vector of images.</p>
		 *
		 * <p>Each image info will consist of the image and its size
		 * in bytes:</p>
		 *
		 *    e.g. chess_icon
		 *
		 * @return   Vector of image strings.
		 */
		public static function getImageProperties():HashMap
		{
			var properties:HashMap=new HashMap();
			var keys:Array=null;

//			if (res != null)
//				keys=res.getBundleNamesForLocale("en_US").;
//			else
			keys=DGLabels.getInstance().listkeys();
			var len:int=keys.length;
			for (var i:int=0; i < len; i++)
			{
				var key:String=String(keys[i]);
				if (key.indexOf("image.") == 0)
				{
					properties.put(key, DGLabels.getInstance().getValue(key));
				}

			}

			// Add special images, if they aren't already defined in the game.properties file
			var gameId:String=GameProperties.getGameID();
			if (properties.containsKey(GameProperties.IMAGE_DG_TITLE) == false)
			{
				properties.put(GameProperties.IMAGE_DG_TITLE, "images/dg_title.gif");
			}
			if (properties.containsKey(GameProperties.IMAGE_GAME_TITLE) == false)
			{
				properties.put(GameProperties.IMAGE_GAME_TITLE, "images/" + gameId + "_title.gif");
			}
			if (properties.containsKey(GameProperties.IMAGE_GAME_ICON) == false)
			{
				properties.put(GameProperties.IMAGE_GAME_ICON, "images/" + gameId + "_icon.gif");
			}

			return properties;
		}

		/**
		 * Constructor for the singleton GameImages.
		 */
		public function GameImages()
		{
			// Retrieve vector of image information from "game_labels.properties".
			imageHash=getImageProperties();
			var loader:LoaderCollection=new LoaderCollection();
			loader.addEventListener(LoaderCollectionEvent.ALLLOADERCOMPLETED, loaderCompleted);
			loader.init(imageHash);

		}

		private function loaderCompleted(evnt:LoaderCollectionEvent):void
		{
			imageHash=evnt.loadedArray;
			finishedLoading=true;

		}

		/**
		 * Method to load the images (simply calls singleton constructor).
		 */
		public static function loadImages():void
		{
			getInstance(); // call constructor
		}

		/**
		 * Return an instance of the GameImages class.
		 *
		 * @return
		 */
		public static function getInstance():GameImages
		{
			if (instance == null)
			{
				instance=new GameImages();
				return instance;
			}
			else
			{
				return instance;
			}
		}



		/**
		 * Method to add images.
		 *
		 * @param images
		 */
		public static function addImage(key:String, path:String):void
		{

			getInstance().imageHash.put(key, path);
		}




		/**
		 * Return a image from a specified index.
		 *
		 * @param index   Index of image to return.
		 * @return        Returns image or null if still loading images.
		 */
		public static function getImage(key:String):BitmapData
		{
			key="image." + key;
//			var path:String=.getValue(key);

			return getInstance().imageHash.getValue(key);
		}

		/**
		 * Return a image from a specified index.
		 *
		 * @param index   Index of image to return.
		 * @return        Returns image or null if still loading images.
		 */
		public static function getImageByIndex(index:int):BitmapData
		{
			var path:BitmapData=getImage(index + "");
			return path;
		}


		/**
		 * Inform if the images have finished loading.
		 *
		 * @return  Returns state of finishedLoading variable.
		 */
		public static function isFinishedLoading():Boolean
		{
			return getInstance().finishedLoading;
		}

		/**
		 * Set true that this class has finished loading.
		 */
		public static function finishedLoaded():void
		{
			getInstance().finishedLoading=true;
		}
	}
}